<?php
class usermanagecontroller extends CI_Controller{	
	function __construct(){
		parent::__construct();
		$this->load->model('user_model');
		$this->load->model('usergrade_model');
	}
	
	function index(){		
		if(isset($_POST['tambahuser']))
		{
			$userid = $_POST['txtUserId'];
			$fullname = $_POST['txtFullName'];
			if(isset($_POST['txtPassword']))
			{
				$password = $_POST['txtPassword'];
			}			
			$grade = !($_POST['grade'] == 0) ? strval($_POST['grade']) : '\'0\'';
			$action = $_POST['hdnaction'];
			
			$this->user_model->setUserId($userid);
			$this->user_model->setFullName($fullname);
			$this->user_model->setGrade($grade);			
			
			if($action == 'i'){				
				$usercount = $this->user_model->getUserByUserId();
				$data = $usercount[0]->retval;
				if(intval($data) > 0){					
					$password = $_POST['txtPassword'];
					$this->user_model->setPassword(md5($password));
					
					$this->user_model->setActive('1');
					$this->user_model->setLastUpdateBy($this->session->userdata('userid'));
					$this->user_model->setLastUpdateOn();
					$this->user_model->update();	
				}else{
					$this->user_model->setPassword(md5($password));		
					$this->user_model->setBeforePassword('');
					$this->user_model->setCreatedBy($this->session->userdata('userid'));
					$this->user_model->setCreatedOn();
					$this->user_model->insert();
				}
			}
			else
			{							
				if(isset($_POST['txtPassword']))
				{
					$password = $_POST['txtPassword'];
					$this->user_model->setPassword(md5($password));
				}										
				$this->user_model->setBeforePassword('');				
				$this->user_model->setLastUpdateBy($this->session->userdata('userid'));
				$this->user_model->setLastUpdateOn();
				
				$this->user_model->update();
			}
			
			$data = array(
				'users' => $this->user_model->getUserList(),
				'gradelist' => $this->usergrade_model->getGradeList(),
				'message' => 'Data user berhasil disimpan',
				'page_title' => 'User Management',
				'name' => $this->session->userdata('fullname')
			);
		}else{
			$data = array(
				'users' => $this->user_model->getUserList(),
				'gradelist' => $this->usergrade_model->getGradeList(),
				'message' => '',
				'page_title' => 'User Management',
				'name' => $this->session->userdata('fullname')
			);
		}
		$content = array('content' => 'master/user/list');
		$this->template->load('templates/default',$content,$data);
	}
	
	function editUser(){		
		$userid = $this->uri->segment(3);
		$this->user_model->setUserId($userid);
		$usercount = $this->user_model->getUserByUserIds();
		$data = array(
			'users' => $this->user_model->getUserByUserIds(),
			'gradelist' => $this->usergrade_model->getGradeList(),
			'message' => '',
			'page_title' => 'User Management',
			'name' => $this->session->userdata('fullname')
		);
		$content = array('content' => 'master/user/edituser');
		$this->template->load('templates/default',$content,$data);
	}
	
	function addUser(){
		$data = array(
			//'users' => $this->user_model->getUserByUserIds(),
			'gradelist' => $this->usergrade_model->getGradeList(),
			'message' => '',
			'page_title' => 'User Management',
			'name' => $this->session->userdata('fullname')
		);
		$content = array('content' => 'master/user/edituser');
		$this->template->load('templates/default',$content,$data);
	}

	function deleteUser(){		
		$userid = $_POST['user'];
		$this->user_model->setUserId($userid);
		$this->user_model->setActive(strval("'".$_POST['active']."'"));
		$this->user_model->update();
		
		$data = array(
			'users' => $this->user_model->getUserList(),
			'gradelist' => $this->usergrade_model->getGradeList(),
			'message' => 'Data user berhasil dihapus',
			'page_title' => 'User Management',
			'name' => $this->session->userdata('fullname')
		);
		$content = array('content' => 'master/user/list');
		$this->template->load('templates/default',$content,$data);
	}
}